package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.HomeIndexCorrectorConvert;
import com.engine4cloud.tdf.supervision.dto.result.*;
import com.engine4cloud.tdf.supervision.mapper.HomeIndexMapper;
import com.engine4cloud.tdf.supervision.dto.result.*;
import com.engine4cloud.tdf.supervision.service.HomeIndexService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeIndexServiceImpl implements HomeIndexService {
    @Resource
    private RemoteDeptService remoteDeptService;

    @Resource
    private HomeIndexMapper homeIndexMapper;

    @Override
    public HomeIndexTaskResultDTO taskCount() {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return homeIndexMapper.taskCount(deptIdList);
    }

    @Override
    public HomeIndexCorrectorResultDTO count() {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return HomeIndexCorrectorConvert.INSTANCE.convert(homeIndexMapper.count(deptIdList));
    }

    @Override
    public List<HomeIndexCorrectorSexRateResultDTO> sexRate() {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return homeIndexMapper.sexRate(deptIdList);
    }

    @Override
    public List<HomeIndexCrimeCategoryResultDTO> crimeCategory() {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return homeIndexMapper.crimeCategory(deptIdList);
    }

    @Override
    public List<HomeIndexAgeRangeResultDTO> ageRange() {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        return homeIndexMapper.ageRange(deptIdList);
    }

    @Override
    public PageInfo<HomeIndexSpResultDTO> taskPage(Params pageable) {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(SecurityUtils.getUser().getDeptId(), SecurityConstants.FROM_IN);
        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        IPage<HomeIndexSpResultDTO> page = homeIndexMapper.taskPage(Pages.page(pageable), deptIdList);
        return Pages.convert(page);
    }
}